#include "BoneReader.h"
#include "io.h"

BoneReader::BoneReader(tstring filename)
	:TextReader(filename),
	m_pCurrentBone(NULL),
	m_pCurrentNode(NULL)
{

}

BoneReader::~BoneReader(void)
{
}


void BoneReader::ParseBone(xml_node<tchar> *bone)
{
	tstring sname,ssid;
	for (xml_attribute<tchar> *attr = bone->first_attribute();
		attr; attr = attr->next_attribute())
	{
		tstring name(attr->name());
		if ( name == _T("name") ){
			sname.assign(attr->value());
		}else if ( name == _T("id")){
			ssid.assign(attr->value());
		}
	}
	m_pCurrentBone = new SkeletonNode(sname,this->ParseInt(ssid));
	if ( m_pCurrentNode != NULL )
		m_pCurrentNode->AddNode(m_pCurrentBone);
	m_pCurrentNode = m_pCurrentBone;
	m_NodeStack.push(m_pCurrentBone);

	// parse pivot element
	xml_node<tchar> *pivot = bone->first_node(_T("pivot"));
	ParsePivot(pivot);
	// parse transform element.
	xml_node<tchar> *transform = bone->first_node(_T("transform"));
	ParseTransform(transform);
	// parse child bones
	for ( xml_node<tchar> *childbone = bone->first_node(_T("bone"));
		childbone; childbone = childbone->next_sibling())
	{
		ParseBone(childbone);
	}
	// reset the stack
	m_NodeStack.pop();
	if ( m_NodeStack.empty() ){
		m_pSkeleton->AddRoot(m_pCurrentNode);
		m_pCurrentNode = NULL;
	}else{
		m_pCurrentBone = NULL;
		m_pCurrentNode = m_NodeStack.top();
	}
}

void BoneReader::ParsePivot(xml_node<tchar> *pivot)
{
}

void BoneReader::ParseTransform(xml_node<tchar> *transform)
{
	tstring tx,ty,tz,rx,ry,rz,sx,sy,sz;
	int i  =0;

	for (xml_attribute<tchar> *attr = transform->first_attribute();
		attr; attr = attr->next_attribute())
	{
		tstring name(attr->name());
		if ( name == _T("tx") ){
			tx.assign(attr->value());
		}else if ( name == _T("ty")){
			ty.assign(attr->value());
		}else if ( name == _T("tz")){
			tz.assign(attr->value());
		}else if ( name == _T("rx")){
			rx.assign(attr->value());
		}else if ( name == _T("ry")){
			ry.assign(attr->value());
		}else if ( name == _T("rz")){
			rz.assign(attr->value());
		}else if ( name == _T("sx")){
			sx.assign(attr->value());
		}else if ( name == _T("sy")){
			sy.assign(attr->value());
		}else if ( name == _T("sz")){
			sz.assign(attr->value());
		}
	}
	m_pCurrentBone->SetTransform(ParseFloat(tx),ParseFloat(ty),ParseFloat(tz),
		ParseFloat(rx),ParseFloat(ry),ParseFloat(rz),
		ParseFloat(sx),ParseFloat(sy),ParseFloat(sz));
}

bool BoneReader::Read()
{
	tstring result;
	tstring line;
	tifstream myfile(GetFileName().c_str());
	if (myfile.is_open())
	{
		while (! myfile.eof() )
		{
			getline (myfile,line);
			result += line;
			result += _T("\n");
		}
		xml_document<tchar> doc;
		tchar* toParse = (tchar*)result.c_str();
		doc.parse<0>(toParse);
		xml_node<tchar> *root = doc.first_node(_T("skeleton"));
		
		m_pSkeleton = new Skeleton();
		
		for ( xml_node<tchar> *bone = root->first_node(_T("bone"));
			bone; bone = bone->next_sibling())
		{
			ParseBone(bone);
		}
		myfile.close();
		return true;
	}
	return false;
}